﻿using System;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Drawing;
using XQ.Framework.core;

namespace XQ.Framework.core
{
    /// <summary>
    /// 验证帮助类
    /// </summary>
    public class ValidateHelper
    {
        /// <summary>
        /// 是否为邮箱名
        /// </summary>
        public static bool IsEmail(string s)
        {
            return ValidateHelper.IsPlumpString(s) && RegexHelper.IsMatch(s, RegexHelper.EMAIL);
        }

        /// <summary>
        /// 是否为手机号
        /// </summary>
        public static bool IsMobile(string s)
        {
            return ValidateHelper.IsPlumpString(s) && RegexHelper.IsMatch(s, RegexHelper.MOBILEPHONE);
        }

        /// <summary>
        /// 是否为固话号
        /// </summary>
        public static bool IsPhone(string s)
        {
            return ValidateHelper.IsPlumpString(s) && RegexHelper.IsMatch(s, RegexHelper.PHONE);
        }

        /// <summary>
        /// 是否为IP
        /// </summary>
        public static bool IsIP(string s)
        {
            return ValidateHelper.IsPlumpString(s) && RegexHelper.IsMatch(s, RegexHelper.IP);
        }

        /// <summary>
        /// 是否为日期
        /// </summary>
        public static bool IsDate(string s)
        {
            return ValidateHelper.IsPlumpString(s) && RegexHelper.IsMatch(s, RegexHelper.DATE);
        }

        /// <summary>
        /// 是否是数值(包括整数和小数)
        /// </summary>
        public static bool IsNumeric(string s)
        {
            return ValidateHelper.IsPlumpString(s) && RegexHelper.IsMatch(s, RegexHelper.INT);
        }

        //public static bool IsNum(string s)
        //{
        //    if (!ValidateHelper.IsPlumpString(s))
        //    {
        //        return false;
        //    }
        //    char[] nums = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        //    char[] chs = s.ToCharArray();
        //    for (int i = 0; i < chs.Length; ++i)
        //    {
        //        if (!IsInRange(chs[i], nums))
        //        {
        //            return false;
        //        }
        //    }
        //    return true;
        //}

        /// <summary>
        /// 判断是数字，空返回false
        /// </summary>
        /// <param name="chs"></param>
        /// <returns></returns>
        public static bool IsNum(params char[] chs)
        {
            if (!ValidateHelper.IsPlumpArray(chs))
            {
                return false;
            }
            for (int i = 0; i < chs.Length; ++i)
            {
                if (!(chs[i] >= '0' && chs[i] <= '9'))
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 判断是数字，空返回false
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsNum(string s)
        {
            if (!ValidateHelper.IsPlumpString(s))
            {
                return false;
            }
            return IsNum(s.ToCharArray());
        }

        //char[] chs = new char[] 
        //{ 
        //    'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
        //    'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
        //};
        //chs.Contains(c);
        /// <summary>
        /// 判断是字母，空返回false
        /// </summary>
        /// <param name="chs"></param>
        /// <returns></returns>
        public static bool IsChar(params char[] chs)
        {
            if (!ValidateHelper.IsPlumpArray(chs)) { return false; }
            for (int i = 0; i < chs.Length; ++i)
            {
                if (!((chs[i] >= 'a' && chs[i] <= 'z') || (chs[i] >= 'A' && chs[i] <= 'Z')))
                { return false; }
            }
            return true;
        }
        /// <summary>
        /// 判断是字母，空返回false
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsChar(string s)
        {
            if (!ValidateHelper.IsPlumpString(s))
            {
                return false;
            }
            return IsChar(s.ToCharArray());
        }
        /// <summary>
        /// 判断是字母或者数字，空返回false
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsCharOrNum(string s)
        {
            if (!ValidateHelper.IsPlumpString(s))
            {
                return false;
            }
            char[] chs = s.ToCharArray();
            for (int i = 0; i < chs.Length; ++i)
            {
                if (!(IsChar(chs[i]) || IsNum(chs[i])))
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 是否为邮政编码
        /// </summary>
        public static bool IsZipCode(string s)
        {
            return ValidateHelper.IsPlumpString(s) && RegexHelper.IsMatch(s, RegexHelper.ZIPCODE);
        }
        /// <summary>
        /// 判断是否是中文字符
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static bool IsChineaseChar(char c)
        {
            return c >= 0x4e00 && c <= 0x9fbb;
        }
        /// <summary>
        /// 判断是否是中文字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsChineaseStr(string str)
        {
            if (!ValidateHelper.IsPlumpString(str))
            {
                return false;
            }
            for (int i = 0; i < str.Length; ++i)
            {
                if (!ValidateHelper.IsChineaseChar(str[i]))
                {
                    return false;
                }
            }
            return true;
        }
        /// <summary>
        /// 简单判断是否是json，不一定全部正确
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static bool IsJson(string json)
        {
            json = ConvertHelper.getString(json);
            return (json.StartsWith("{") && json.EndsWith("}"))
                || (json.StartsWith("[") && json.EndsWith("]"));
        }
        /// <summary>
        /// 是否是图片
        /// </summary>
        /// <param name="urlOrPathOrName"></param>
        /// <returns></returns>
        public static bool IsImage(string urlOrPathOrName)
        {
            urlOrPathOrName = ConvertHelper.getString(urlOrPathOrName).Trim().ToLower();
            for (int i = 0; i < StaticDataBase.imageExtesions.Length; ++i)
            {
                if (urlOrPathOrName.EndsWith(StaticDataBase.imageExtesions[i])) { return true; }
            }
            return false;
        }
        /// <summary>
        /// 检查直接数组是否是可以转换为图片
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static bool IsImage(byte[] b)
        {
            if (!ValidateHelper.IsPlumpArray(b)) { return false; }
            MemoryStream ms = null;
            Image img = null;
            try
            {
                ms = new MemoryStream(b);
                img = Image.FromStream(ms);
                return !img.Size.IsEmpty;
            }
            catch (Exception e)
            {
                return false;
            }
            finally
            {
                if (img != null) { img.Dispose(); }
                XQ.Framework.io.IOHelper.closeStream(ms);
            }
        }

        #region 判断是否是空数据
        public static bool IsPlumpArray<T>(T[] arr)
        {
            return arr != null && arr.Length > 0;
        }
        public static bool IsPlumpList<T>(IList<T> list)
        {
            return list != null && list.Count > 0;
        }
        public static bool IsPlumpDict<K, V>(IDictionary<K, V> dict)
        {
            return (dict != null && dict.Count > 0);
        }

        public static bool IsPlumpStringAfterTrim(string str)
        {
            return IsPlumpString(str) && str.Trim().Length > 0;
        }

        public static bool IsPlumpString(string str)
        {
            return str != null && str.Length > 0;
        }

        public static bool IsAllPlumpString(params string[] strs)
        {
            if (!IsPlumpArray<string>(strs)) { return false; }
            for (int i = 0; i < strs.Length; ++i)
            {
                if (!IsPlumpString(strs[i]))
                {
                    return false;
                }
            }
            return true;
        }
        #endregion

        /// <summary>
        /// 判断字符串的长度是否在范围之内，str可以为空
        /// </summary>
        /// <param name="str"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <param name="withBorder"></param>
        /// <returns></returns>
        public static bool IsInRange(string str, int min, int max, bool withBorder = true)
        {
            str = ConvertHelper.getString(str);
            if (withBorder && (str.Length == min || str.Length == max)) { return true; }
            return str.Length > min && str.Length < max;
        }
        /// <summary>
        /// 判断对象是否在数组内
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="array"></param>
        /// <returns></returns>
        public static bool IsInRange<T>(T obj, params T[] array)
        {
            if (ValidateHelper.IsPlumpArray(array))
            {
                return array.Contains(obj);
            }
            return false;
        }
        /// <summary>
        /// 判断两个集合是否有交集
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static bool HasInterSection<T>(IList<T> a, IList<T> b)
        {
            if (!ValidateHelper.IsPlumpList(a) || !ValidateHelper.IsPlumpList(b)) { return false; }
            return a.Count(x => b.Contains(x)) > 0;
        }

        public static bool EqualIgnoreCase(string a, string b)
        {
            if (a != null && b != null)
            {
                return StringHelper.Trim(a).ToLower() == StringHelper.Trim(b).ToLower();
            }
            return true;
        }

        public static bool Is<T>(object obj)
        {
            return obj != null && obj is T;
        }
    }
}
